package com.kirito.demo.action;

import com.kirito.demo.IServiceDemo;
import com.kirito.mvcframework.Annotation.Autowired;
import com.kirito.mvcframework.Annotation.Controller;
import com.kirito.mvcframework.Annotation.RequestMapping;
import com.kirito.mvcframework.Annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName DemoAction
 * @Description: TODO
 * @Author kirito
 * @Date 2021/1/7
 * @Version V1.0
 **/
@Controller
@RequestMapping("/demo")
public class DemoAction {

    @Autowired
    private IServiceDemo serviceDemo;

    @RequestMapping("/query")
    public void query(HttpServletRequest req , HttpServletResponse resp, @RequestParam("name")String name){
        String result =serviceDemo.get(name);
//        String result = "my name is "+ name;
        try {
            resp.getWriter().write(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/add")
    public void add(HttpServletRequest req, HttpServletResponse resp, @RequestParam("a") Integer a, @RequestParam("b") Integer b){
        try {
            resp.getWriter().write(a + "+" + b + "=" + (a + b));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/sub")
    public void add(HttpServletRequest req, HttpServletResponse resp,
                    @RequestParam("a") Double a, @RequestParam("b") Double b){
        try {
            resp.getWriter().write(a + "-" + b + "=" + (a - b));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/remove")
    public String  remove(@RequestParam("id") Integer id){
        return "" + id;
    }
}
